# -*- coding: UTF-8 -*-
from typing import List
from leetcode_helper import TestcaseHelper, SolutionBase


class Solution(SolutionBase):
    # 解题的方法，命名和Leetcode的保持一致即可
    def findSubstring_backtrace(self, s: str, words: List[str]) -> List[int]:
        def backtrack(words: List[str], visited: List, new_strs: List[int], index_result: List[int]):
            if len(words) == len(new_strs):
                sub_str = ''.join(new_strs)
                sub_str_length = len(sub_str)
                for i in range(0, len(s) - sub_str_length + 1):
                    if s[i: i + sub_str_length] == sub_str:
                        index_result.append(i)
                return

            for i in range(len(words)):
                if visited[i]:
                    continue
                if i > 0 and words[i] == words[i - 1] and visited[i - 1] is False:
                    continue

                visited[i] = True
                new_strs.append(words[i])
                backtrack(words, visited, new_strs, index_result)
                new_strs.pop()
                visited[i] = False

        ans = []
        words = sorted(words)
        visited = [False for _ in range(len(words))]
        backtrack(words, visited, [], ans)
        return ans

    def findSubstring(self, s: str, words: List[str]) -> List[int]:
        ans = []
        single_word_length = len(words[0])
        word_count = len(words)
        words_length = word_count * single_word_length

        words_dict = {}
        for word in words:
            words_dict[word] = words_dict.get(word, 0) + 1

        for i in range(0, len(s) - words_length + 1):
            sub_string = s[i: i + words_length]
            match_dict = {}
            start, end = 0, single_word_length
            match = True

            while True:
                temp_sub = sub_string[start: end]
                if words_dict.get(temp_sub, -1) == -1:
                    match = False
                    break

                match_dict[temp_sub] = match_dict.get(temp_sub, 0) + 1
                start = end
                end += single_word_length

                if end > words_length:
                    break

            if match:
                for word, value in match_dict.items():
                    if words_dict.get(word, -1) != value:
                        match = False
                        break

            if match:
                ans.append(i)

        return ans

    def check_result(self, expect, result) -> bool:
        return self.check_result_array(expect, result)


if __name__ == '__main__':
    solution = Solution()

    # 按照leetcode的题目说明，将基本的test case填入
    # 每条case的最后一个元素是正确的结果，即期待的结果
    # 使用元组来保存输入的数据，期待的结果
    testcases = [
        ("wordgoodgoodgoodbestword", ["word", "good", "best", "good"], [8]),
        ('foobarfoobar', ['foo', 'bar'], [0, 3, 6]),
        ("barfoothefoobarman", ["foo", "bar"], [0, 9]),
        ("wordgoodgoodgoodbestword", ["word", "good", "best", "word"], []),
        ("barfoofoobarthefoobarman", ["bar", "foo", "the"], [6, 9, 12])
    ]

    for case_item in testcases:

        input1 = case_item[0]
        input2 = case_item[1]

        expect = case_item[len(case_item) - 1]

        exec_result = solution.findSubstring(input1, input2)

        # 判断执行结果，输出提示信息
        check_result = solution.check_result(expect, exec_result)
        TestcaseHelper.print_case(check_result, case_item, exec_result)

